<?php defined('SYSPATH') OR die('No direct access allowed.');
class Code_Controller extends Base_Controller
{
	public function detail($id,$r=0)
	{
		$id = (int)$id;
		$code = ORM::factory('code')->find($id);
		if(!empty($code->id))
		{
			$codeArr = $code->as_array();
			
			$codeArr['brushLan'] = code::getBrush($code->lan);
			$this->template->head->title = $code->title;
			$this->template->content = new View('detail');
			$this->template->content->code = $codeArr;
			$this->template->content->bind('codeList',$codeList);
			$this->template->content->bind('snippet',$snippet);
			$this->template->content->bind('snippetLength',$snippetLength);
			$this->template->content->bind('currentR',$currentR);
			$this->template->content->bind('saved',$saved);
			$this->template->content->bind('savedNum',$savedNum);
			$this->template->content->bind('cmts',$cmts);
			$this->template->content->bind('user',$user);

			//载入css和js
			if($brush = code::getBrushFile($codeArr['brushLan']))
			{
				javascript::add(array($brush));
			}

			//查看该用户是否已保存过该信息
			$saved = false;
			if($userId = user::is_login())
			{
				$user = ORM::factory('user',$userId);
				if($user->has(ORM::factory('code',$code->id)))
				{
					$saved = true;
				}
			}
			//获得该用户的信息
			$user = $code->user->as_array();

			//该用户发表的其他代码
			$codeLists = ORM::factory('code')->select('id','title')->where(array('user_id'=>$user['id'],'id!='=>$code->id))->find_all(5,0);
			$codeList = array();
			foreach($codeLists as $val)
			{
				$codeList[] = $val;
			}

			//收藏数
			$savedNum = $code->users->count();

			//用户评论
			$cmts = $code->comments;

			$snippetArr = array();
			foreach($code->snippets as $snippet)
			{
				$snippetArr[] = $snippet->as_array();
			}

			$currentR = $snippetLength = count($snippetArr);
			if($r==0)
			$snippet = $snippetArr[0];
			else
			{
				$r = (int)$r;
				$snippetArr = array_reverse($snippetArr);
				if($r>count($snippetArr)) $this->_popMsg(Kohana::lang('code.noteexists'));
				$snippet = $snippetArr[$r-1];
				$currentR = $r;
			}

		}
		else
		{
			$this->_popMsg(Kohana::lang('code.notexists'));
		}
	}

	/**
	 * 某一分类下的所有列表
	 * @param string 分类名，如:php,javascript等等
	 * @return object
	 */
	public function lan($cate,$page=1)
	{
		$this->template->head->title = $cate.'的代码列表';
		$this->template->content = new View('cate');
		$this->template->content->bind('list',$list);
		$this->template->content->bind('pagination',$pagination);
		$this->template->content->lan = $cate;
		$code = ORM::factory('code');
		$page = (int)$page<1?1:(int)$page;
		$num = $code->where('lan',$cate)->count_all();
		$p = Kohana::config('pagination');
		$items_per_page = $p['default']['items_per_page'];
		$total_pages = floor(($num/$items_per_page)+1);
		$page = ($page>$total_pages)?$total_pages:$page;
		$list = $code->where('lan',$cate)->orderby('id','desc')->find_all($items_per_page,($page-1)*$items_per_page);
		$pagination = new Pagination(array(
			'total_items'	=>	$num,
			'style'			=>	'digg',
			)
		);
	}

	/**
	 * 搜索
	 */
	public function search($uri_segment='page',$page=1)
	{
		$items_per_page = 10;
		$keyword = $_GET['keyword'];
		$lan = $_GET['lan'];
		$page = (int)$page<1?1:(int)$page;
		$this->template->head->title = $keyword.'的搜索结果';
		$this->template->content = new View('search');
		$this->template->content->bind('list',$result);
		$this->template->content->bind('pagination',$pagination);
		$this->template->content->keyword = $keyword;

		$code = ORM::factory('code');
		if($lan)
		{
			$total_nums = $code->like('title',$keyword.'%')->where('lan',$lan)->count_all();
			$total_pages = floor($total_nums/$items_per_page+1);
			$page = $page>$total_pages?$total_pages:$page;
			$result = $code->like('title',$keyword)->where('lan',$lan)->orderby('id','desc')->find_all($items_per_page,($page-1)*$items_per_page);
		}
		else
		{
			$total_nums = $code->like('title',$keyword)->count_all();
			$total_pages = floor($total_nums/$items_per_page+1);
			$page = $page>$total_pages?$total_pages:$page;
			$result = $code->like('title',$keyword)->orderby('id','desc')->find_all($items_per_page,($page-1)*$items_per_page);
		}

		$pagination = new Pagination(array(
			'total_items'	=>	$total_nums,
			'items_per_page'=>	$items_per_page,
			)
		);
	}

}
